package com.jinkaimen.kqb.ui.weiget;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.utils.AppUtils;
import com.jinkaimen.kqb.utils.DensityUtil;
import com.jinkaimen.kqb.utils.ViewUtils;


/**
 * Created by wei.shuxin on 2016/12/30.
 * <p>
 * 用于存放一些通用的按钮
 */

public class CustomButtons {


    /**
     * titleBar中用的TextView
     *
     * @param context
     * @param textContent 默认文字为""
     * @return
     */
    public static TextView getTitleBarTvBtn(Context context, String textContent) {
        TextView textView = new TextView(context);
        textView.setText(TextUtils.isEmpty(textContent) ? "" : textContent);
        textView.setMinWidth(context.getResources().getDimensionPixelOffset(R.dimen.title_bar_height));
        textView.setHeight(R.dimen.title_bar_height);
        int padding = context.getResources().getDimensionPixelOffset(R.dimen.title_bar_tvbtn_padding);
        textView.setPadding(padding, 0, padding, 0);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(context.getResources().getColor(R.color.white));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.title_bar_tvbtn_size));
        textView.setCompoundDrawablePadding(context.getResources().getDimensionPixelOffset(R.dimen.title_bar_tvbtn_drawable_padding));
        textView.setId(AppUtils.generateViewId());
        return textView;
    }

    /**
     * titleBar中用的返回按钮
     *
     * @param context
     * @return
     */
    public static TextView getTitleBarBackBtn(Context context) {
        TextView textView = getTitleBarTvBtn(context, "返回");
        ViewUtils.addDrawable2TvById(textView, R.mipmap.back, 1);
        return textView;
    }

    /**
     * titleBar中用的纯文字按钮
     *
     * @param context
     * @param textContent 文字内容
     * @return
     */
    public static TextView getTitleBarNoIconBtn(Context context, String textContent) {
        TextView textView = getTitleBarTvBtn(context, textContent);
        return textView;
    }

    /**
     * titleBar中使用ImageView按钮
     *
     * @param context
     * @param resId   按钮图片id
     * @return
     */
    public static ImageView getTitleBarIvBtn(Context context, int resId) {
        ImageView imageView = new ImageView(context);
        int title_bar_height = context.getResources().getDimensionPixelOffset(R.dimen.title_bar_height);
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(title_bar_height, title_bar_height));
        imageView.setImageResource(resId);
        imageView.setId(AppUtils.generateViewId());
        return imageView;
    }


    /**
     * 获取dialog按钮
     *
     * @param context
     * @param btnText
     * @return
     */
    public static TextView getDialogTvBtn(Context context, String btnText) {
        TextView textView = new TextView(context);
        textView.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
        int padding = (int) DensityUtil.applyDimension(context, TypedValue.COMPLEX_UNIT_DIP, 10);
        textView.setPadding(padding, padding, padding, padding);
        textView.setGravity(Gravity.CENTER);
        textView.setLines(1);
//        textView.setBackgroundResource(R.drawable.selector_dialog_item_bg_white_gray);
        textView.setTextColor(context.getResources().getColor(R.color.dialog_text_blue));
        if (!TextUtils.isEmpty(btnText)) {
            textView.setText(btnText);
        }
        textView.setId(AppUtils.generateViewId());
        return textView;
    }

    /**
     * 获取pop条目
     *
     * @param context
     * @param btnText 文本
     * @param tvColor 文本颜色,默认dialog_text_blue
     * @return
     */
    public static TextView getPopTvItem(Context context, String btnText, int tvColor) {
        TextView textView = new TextView(context);
        textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) context.getResources().getDimension(R.dimen.dialog_item_heghit), 1.0f));
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,context.getResources().getDimension(R.dimen.dialog_text_content));
        textView.setBackgroundResource(R.drawable.selector_dialog_item_bg_white_gray);
        textView.setLines(1);
        try {
            if (tvColor != -1) {
                textView.setTextColor(context.getResources().getColor(tvColor));
            } else {
                textView.setTextColor(context.getResources().getColor(R.color.dialog_text_blue));
            }
            if (!TextUtils.isEmpty(btnText)) {
                textView.setText(btnText);
            }
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        textView.setId(AppUtils.generateViewId());
        return textView;
    }
}
